# -*- coding: utf-8 -*-
import pandas as pd
import pickle
import os
from pathlib import Path
import sys
current_dir = Path(__file__).resolve().parent
project_root = current_dir.parent
sys.path.append(str(project_root))
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, confusion_matrix
from tqdm import tqdm
from settings.path import *


if not os.path.exists(path.path_model):
            os.mkdir(path.path_model)


data = pd.read_csv(path_train_processed_txt)
words = data['word']
labels = data['label']
stop_woeds = open(path_stopword_txt, encoding='utf-8').read().split()
tfidf = TfidfVectorizer(stop_words=stop_woeds)  # 文本特征提取模块
features = tfidf.fit_transform(words)
X_train, X_test, y_train, y_test = train_test_split(features, labels,test_size=0.2, random_state=42)
model = RandomForestClassifier(n_jobs=-1)
for _ in tqdm(range(1),desc='模型训练进度》》》》'):
    model.fit(X_train, y_train)
y_pred = model.predict(X_test)
print(f'模型评估：{y_pred}')
print("准确率:", accuracy_score(y_test, y_pred))
print("精确率 (micro):", precision_score(y_test, y_pred, average='micro'))
print("召回率 (micro):", recall_score(y_test, y_pred, average='micro'))
print("F1分数 (micro):", f1_score(y_test, y_pred, average='micro'))
print("保存模型")
with open(path_random_tree_model,'wb') as f:
    pickle.dump(model, f)
with open(path_modol_random_tree_vectorizer,'wb') as f:
    pickle.dump(features, f)
print('保存成功')
